<?php namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;

class UsersController extends Controller
{
  public function __construct()
  {
    $this->middleware('admin');
  }

  public function index()
  {
    $data['users'] = User::orderBy('id', 'desc')->paginate(15);
    $data['title'] = '会员管理';
    return view('admin.users.index', $data);
  }

  public function postSetAdmin($id = 0)
  {
    if ($id > 1) {
      $user = User::find($id);
      if ($user) {
        $user->is_admin = ($user->is_admin == 1) ? 0 : 1;
        $user->save();
      }
    } else {
      return redirect()->back()->withErrors(['error' => '网站创始人无法解除管理权限']);
    }

    return redirect()->back();
  }

}
